;$(function () {
    layui.use(['form', 'table'], function () {
        let table = layui.table,
            form = layui.form;

        table.render({
            elem: "#authorityInfoTable"
            , url: "getRoleInfo"
            , method: "post"
            , response: {
                statusCode: true
            }
            , defaultToolbar: []
            , cellMinWidth: 100
            , cols: [[
                {field: 'sorts', title: '序号', width: 60, templet: '#sort', align: 'center'}
                , {field: 'role', title: '角色', align: 'center'}
                , {field: 'createAt', title: '创建时间', align: 'center'}
                , {field: 'updateAt', title: '更新时间', align: 'center'}
                , {
                    field: 'tool', title: '操作', align: 'center', width: 200,
                    templet: function (d) {
                        return '<span style="text-decoration:underline;cursor:pointer;color:#4BB2FF" ' +
                            'lay-event="authorityUpdate">编辑</span>'
                        + '<span style="margin-left:20px;text-decoration:underline;cursor:pointer;color:#D92E2E"' +
                        ' lay-event="authorityDelete">删除</span>'
                    }
                }
            ]]
            , page: true
            , id: "authorityInfoTableRender"
        });

        table.on('tool(authorityInfoTable)', function (obj) {
            let tableData = obj.data;
            if (obj.event === 'authorityUpdate') {
                layer.open({
                    type: 2,
                    title: "修改角色权限",
                    skin: 'layui-layer-rim',//加边框
                    area: ['400px', '450px'],
                    // content: $("#authorityEdit"),//注意放在<body>最外层
                    content: 'getAuthorityByRoleName?id=' + tableData.id + '&role=' + tableData.role,
                    btn: ['确认', '取消'],

                    yes: function (index, layero) {
                        let checkedBox = [];//定义一个空数组
                        let body = layer.getChildFrame('body', index);
                        let roleName = body.find("input[name='authorityRoleName']").val();
                        body.find("input[name='checkbox']:checked").each(function (i) {//把所有被选中的复选框的值存入数组
                            checkedBox[i] = $(this).val();
                        });
                        $.ajax({
                            type: 'POST',
                            url: 'updateRoleAuthority',
                            async: false,
                            data: {
                                "roleId": tableData.id,
                                "checkedBox": checkedBox.toString(),
                                "roleName": roleName,
                            },
                            dataType: 'JSON',
                            error: function (data) {
                                layer.msg('稍后再试！');
                            },
                            success: function (data) {
                                if (data.code === true) {
                                    layer.msg('修改成功！');
                                    layer.close(index);
                                    table.reload('authorityInfoTableRender', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                        });

                        form.render('checkbox');
                    },
                    btn2: function (index, layero) {
                        layer.close(index);
                    }
                });
            }
            if (obj.event === 'authorityDelete') {
                layer.confirm('真的要删除吗？', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'deleteRole',
                        async: false,
                        data: {"id": tableData.id},
                        dataType: 'JSON',
                        error: function (data) {
                            layer.msg('稍后再试！');
                        },
                        success: function (data) {
                            if (data.code === true) {
                                layer.msg('删除成功！');
                                table.reload('authorityInfoTableRender', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                    });
                    layer.close(index);
                });
            }
        });

        form.on('checkbox(checkbox0)', function (data) {
            let elem = data.elem; //得到checkbox原始DOM对象
            // console.log(data.elem.checked); //是否被选中，true或者false
            // console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            // console.log(data.othis); //得到美化后的DOM对象
            if (!elem.checked) {
                $('input[checkPid="' + elem.value + '"]').prop("checked", false);
            }
            if (elem.checked) {
                $('input[checkPid="' + elem.value + '"]').prop("checked", true);
            }
            form.render();
        });
        form.on('checkbox(checkbox1)', function (data) {
            let elem = data.elem; //得到checkbox原始DOM对象
            if (elem.checked) {
                $('input[checkId="' + elem.getAttribute("checkPid") + '"]').prop("checked", true);
            }
            if (!elem.checked) {
                $('input[checkId="' + elem.getAttribute("checkPid") + '"]').prop("checked", false);
                $('input[checkPid="' + elem.getAttribute("checkPid") + '"]').each(function () {
                    if ($(this).is(':checked')) {
                        $('input[checkId="' + elem.getAttribute("checkPid") + '"]').prop("checked", true);
                        return;
                    }
                })
            }
            form.render();
        });
        $("#authorityCheckboxAll").click(function () {
            $('input[name="checkbox"]').prop("checked", true);
            form.render();
        });
        $("#authorityCheckboxNo").click(function () {
            $('input[name="checkbox"]').prop("checked", false);
            form.render();
        });
        $("#authorityInsertBtn").click(function () {
            layer.open({
                type: 2,
                title: "新建角色",
                skin: 'layui-layer-rim',//加边框
                area: ['400px', '450px'],
                content: 'getAuthorityByRoleName',
                btn: ['确认', '取消'],
                success: function (index, layero) {
                    form.render();
                },
                yes: function (index, layero) {
                    let checkedBox = [];//定义一个空数组
                    let body = layer.getChildFrame('body', index);
                    body.find("input[name='checkbox']:checked").each(function (i) {
                        checkedBox[i] = $(this).val();
                    });
                    let roleName = body.find("input[name='authorityRoleName']").val();
                    $.ajax({
                        type: 'POST',
                        url: 'insertRole',
                        async: false,
                        data: {
                            "roleName": roleName,
                            "authority": checkedBox.toString()
                        },
                        dataType: 'JSON',
                        error: function (data) {
                            layer.msg('稍后再试！');
                        },
                        success: function (data) {
                            if (data.code === true) {
                                layer.msg('添加成功！');
                                layer.close(index);
                                table.reload('authorityInfoTableRender', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                    });
                    form.render('checkbox');
                },
                btn2: function (index, layero) {
                    layer.close(index);
                }
            });

        });

    });
});